import request from 'umi-request';
import type { GetAllUserRes, KeyObj } from '@/constants/index';
import type {
  CompanyType,
  CheckQualityResult,
  CopyrightData,
  PaymentmodeDetail,
  WorksLyricsItem,
  WorksMusicItem,
  SongItem,
  CurrentUser,
} from './type';
import type { AuthItem } from '@/utils/authsdk';

export const defaultSearchParams = {
  current: 1,
  pageSize: 100,
};

export async function queryCurrentUser() {
  return request<Result<CurrentUser>>('/user/getCurrentUser');
}

export async function queryToken(code: string) {
  return request<Result<string>>('/user/tmeloginCallback', {
    method: 'POST',
    data: {
      code,
    },
  });
}

export async function logout() {
  return request<Result<string>>('/logout', {
    method: 'GET',
  });
}

export const getListDefaultParams = (extParams?: KeyObj) => {
  return {
    pageSize: 10,
    current: 1,
    filters: {},
    ...extParams,
  };
};

export const getArtistPage = (keyWord: string) => {
  return request('/singer/page', {
    method: 'POST',
    data: {
      filters: { keyWord },
      ...defaultSearchParams,
    },
  });
};

export const searchCompany = (keyWord: string) => {
  return request<PaginationResult<CompanyType>>('/company/search', {
    method: 'POST',
    data: {
      filters: { keyWord },
      ...defaultSearchParams,
    },
  });
};

export const searchLyrics = (keyWord: string) => {
  return request<Result<WorksLyricsItem[]>>('/lyrics/listByKeyword', {
    method: 'POST',
    data: {
      filters: { keyWord },
      ...defaultSearchParams,
    },
  });
};
export const searchMusic = (keyWord: string) => {
  return request<Result<WorksMusicItem[]>>('/music/listByKeyword', {
    method: 'POST',
    data: {
      filters: { keyWord },
      ...defaultSearchParams,
    },
  });
};

export const searchSong = (keyWord: string) => {
  return request<PaginationResult<SongItem>>('/simigroup/queryTracksByKeyword', {
    method: 'GET',
    params: { keyWord },
  });
};

export const getCheckQualityResult = (fileId: string) => {
  return request<Result<CheckQualityResult[]>>('/track/queryAudioCheckRes', {
    method: 'POST',
    data: [{ fileId }],
  });
};

export const searchContract = (cpCompanyId: number, keyWord: string) => {
  return request<PaginationResult<CopyrightData>>('/copyright/search', {
    method: 'POST',
    data: {
      filters: { cpCompanyId, keyWord },
      ...defaultSearchParams,
    },
  });
};

export const getPaymentModeList = () => {
  return request<Result<PaymentmodeDetail[]>>('/paymentMode/list', {
    method: 'POST',
  });
};

// 获取所有用户
export async function getAllUser(): Promise<GetAllUserRes> {
  return request.get('/oa/user/all');
}

// 获取当前登陆用户的权限列表
export async function getAuthCode(): Promise<Result<AuthItem[]>> {
  return request('/rewirte/auth-api/v1/op/getUserRights');
}

// 获取tme的所有用户
export async function getAllTMEUser(): Promise<GetAllUserRes> {
  return request.get('/oa/user/all');
}
